﻿using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.工厂.车间
{
    public partial class Form产线消耗登记 : UIPage
    {
        private UIRadioButton 当前来源选项 = new UIRadioButton();
        public Form产线消耗登记()
        {
            InitializeComponent();
        }
        private void ListWorkLine(string WorkType, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = @"SELECT '' WorkLineCode,'请选择' WorkLineName
                    union all
                    SELECT   WorkLineCode, WorkLineName
                    FROM      Dic_WorkLine where WorkType='" + WorkType + "' order by WorkLineCode";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            uiComboBox产线.DataSource = tb;
            uiComboBox产线.DisplayMember = "WorkLineName";
            uiComboBox产线.ValueMember = "WorkLineCode";
            uiComboBox产线.Refresh();
        }
        private void Form产线消耗登记_Load(object sender, EventArgs e)
        {
            uiDataGridView临时.AutoGenerateColumns = false;
            uiDatePicker日期.Value = DateTime.Today; 
            //List辅料消耗();
            //显示自己的产线
            ListWorkLineByUser();
            //显示自己的班组
            ListWorkTeamByUser();
        }

        private void ListWorkTeamByUser()
        {
            string sql = "SELECT distinct [班组名称]   FROM [View_用户班组] where 用户名称='" + pub.sys_user.UserTrueName + "' ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBoxWorkTeam.DataSource = tb;
                this.uiComboBoxWorkTeam.DisplayMember = "班组名称";
                this.uiComboBoxWorkTeam.ValueMember = "班组名称";
                this.uiComboBoxWorkTeam.Refresh();
            }
            else
            {
                //MessageBox.Show("您没有可以操作的班组!","提示",MessageBoxButtons.OK);
                this.uiButton保存.Enabled = false;
            }
        }

        private void ListWorkLineByUser()
        {
            string sql;

            sql = "SELECT distinct  [产线名称]         FROM  [View_产线人员] where 用户名称='" + pub.sys_user.UserTrueName + "' ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox产线.DataSource = tb;
                this.uiComboBox产线.DisplayMember = "产线名称";
                this.uiComboBox产线.ValueMember = "产线名称";
                this.uiComboBox产线.Refresh();
            }
            else
            {
                // MessageBox.Show("您没有可以操作的产线!","提示",MessageBoxButtons.OK);
                this.uiButton保存.Enabled = false;
            }
        }

        private void uiRadioButton酸洗_CheckedChanged(object sender, EventArgs e)
        {
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton轧硬_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton镀锌_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);

        }

        private void uiRadioButton彩涂_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton罩退_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton退火_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton硅钢_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiButton选择存货2_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();
            form存货档案.存货分类 = "";
            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox存货编号2.Text = form存货档案.存货档案.T_GOOD_CODE;
                this.uiTextBox存货名称2.Text = form存货档案.存货档案.T_GOOD_NAME;
                this.uiTextBox规格型号2.Text = form存货档案.存货档案.T_GOOD_MODEL;
                this.uiTextBox计量单位1.Text = form存货档案.存货档案.T_GOOD_UNIT;
                //try
                //{
                //    string[] gg = uiTextBox规格型号2.Text.Split("*");
                //    this.uiTextBox计量单位1.Text = gg[0];
                //    this.uiTextBox宽度1.Text = gg[1];
                //    this.uiTextBox虚拟值1.Text = gg[2];
                //}
                //catch
                //{
                //    //MessageBox.Show("规格不符合格式!", "提示", MessageBoxButtons.OK);
                //    //return;
                //}
            }
        }

        private void uiButton保存_Click(object sender, EventArgs e)
        {
            if (!decimal.TryParse(uiTextBox用量.Text, out decimal 数量))
            {
                MessageBox.Show("输入的用量不符合数字格式");
            }
            else
            {
                string sql;
                sql = @" insert into Bus_Consume( ConsumeGuid, ConsumeType,  ConsumeDate,WorkShopCode,  InventoryCode, InventoryName, Size, Batch, QuantityMain,  Unit ,
                         CreateID, Creator, CreateDate,  WorkLineName,TeamName,Status)";
                sql += " values(newid(),'" + this.uiTextBox加工类别.Text + "','" + this.uiDatePicker日期.Value + "','" + pub.全局工厂编号 + "','" + this.uiTextBox存货编号2.Text + "',";
                sql += " '" + this.uiTextBox存货名称2.Text + "','" + this.uiTextBox规格型号2.Text + "','" + this.uiTextBox批号或件号.Text + "',";

                sql += " '" + 数量 + "','" + this.uiTextBox计量单位1.Text + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "',getdate(),'" + this.uiComboBox产线.SelectedText + "','" + this.uiComboBoxWorkTeam.SelectedText + "','未确定')";
                pub.ExecuteSql(pub.ConnectionString, sql, this.uiTextBox加工类别.Text + "辅料消耗塞入");
                List辅料消耗();
            }
            
        }

        private void List辅料消耗()
        {
            DataTable tb = ListData();
            if (tb != null)
            {
                this.uiDataGridView临时.DataSource = tb;
                this.uiDataGridView临时.Refresh();
            }
        }

        private DataTable ListData()
        {
            string sql;
            sql = @" SELECT  row_number() over(order by CreateDate,ConsumeGuid) 序号,  ConsumeGuid, ConsumeType,  ConsumeDate,WorkShopCode,  InventoryCode, InventoryName, Size, Batch, QuantityMain,Unit, 
CreateID, Creator, CreateDate,  WorkLineName,TeamName,Status  " +
                " FROM      Bus_Consume  where 1=1 and ConsumeDate='" + this.uiDatePicker日期.Value + "' and WorkLineName='" + this.uiComboBox产线.SelectedText + "'";
            sql += " and TeamName='" + this.uiComboBoxWorkTeam.SelectedText + "'";
            return pub.GetDataTable(pub.ConnectionString, sql);
        }

        private void uiButton刷新_Click(object sender, EventArgs e)
        {
            if (this.uiComboBoxWorkTeam.Text != "" && this.uiComboBox产线.Text != "" && this.uiTextBox加工类别.Text != "")
            {
                this.uiButton保存.Enabled = true;
                List辅料消耗();
            }
            else
            {
                this.uiButton保存.Enabled = false;
            }

        }

        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            sql = "select  WorkType FROM      Dic_WorkLine where WorkLineName='" + uiComboBox产线.Text + "' ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiTextBox加工类别.Text = tb.Rows[0]["WorkType"].ToString();

            }
            else
            {
                // MessageBox.Show("加工类别不能为空，请选择一下产线","",MessageBoxButtons.OK);
                this.uiButton保存.Enabled = false;
            }
        }

        private void uiDataGridView临时_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //没有确认可以删
            if (this.uiDataGridView临时.Rows.Count > 0)
            {
                if (this.uiDataGridView临时.Columns[e.ColumnIndex].Name == "C删除")
                {
                    string sql;
                    string Cbguid = this.uiDataGridView临时.Rows[e.RowIndex].Cells["Cbguid"].Value.ToString();
                    string 操作人 = this.uiDataGridView临时.Rows[e.RowIndex].Cells["C操作人"].Value.ToString();
                    //检查是不是已经确认
                    if (!是否已经确认(Cbguid))
                    {
                        if (pub.sys_user.UserTrueName == 操作人)
                        {
                            sql = "delete Bus_Consume where ConsumeGuid='" + Cbguid + "' ";
                            pub.ExecuteSql(pub.ConnectionString, sql);
                            List辅料消耗();
                        }
                        else
                        {
                            MessageBox.Show("您不可以删除其他人的操作的记录", "", MessageBoxButtons.OK);
                            return;
                        }

                    }
                    else
                    {
                        MessageBox.Show("已经被确认不能删除了", "", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
        }

        private bool 是否已经确认(string cbguid)
        {
            string sql;
            sql = "select Status from Bus_Consume where ConsumeGuid='" + cbguid + "' ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                if (tb.Rows[0]["Status"].ToString() == "未确定")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
